//
//  ChangeDataViewController.m
//  AddressBook
//
//  Created by mac on 2017/8/26.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ChangeDataViewController.h"

@interface ChangeDataViewController ()

@property (nonatomic ,strong) UITextField *changeText;
@property (nonatomic, strong) NSDictionary *userData;
@property (nonatomic, strong) UserDataModel *model;

@end

@implementation ChangeDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改资料";
    self.view.backgroundColor = [UIColor whiteColor];
    self.model = [UserDataModel getLocalModel];;
//    self.userData = [[NSUserDefaults standardUserDefaults] objectForKey:KUSERINFORMATION];
    if ([self.titleText isEqualToString:@"姓名"] || [self.titleText isEqualToString:@"身份证"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"修改" style:UIBarButtonItemStylePlain target:self action:@selector(changeData)];
    }else if ([self.titleText isEqualToString:@"添加电话"]){
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(changeData)];
    }
    
    [self.view addSubview:self.changeText];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.changeText.frame)-5, ScreenW - 20, 1)];
    line.backgroundColor = MainColor;
    [self.view addSubview:line];
}

-(void)changeData{
    
    if ([self.titleText isEqualToString:@"姓名"]) {
        NSString *member_name = @"member_name";
        [self changeDataSave:member_name NSString:self.changeText.text];
    }else if ([self.titleText isEqualToString:@"身份证"]){
        if (self.changeText.text.length == 18) {
            NSString *id_card = @"id_card";
            [self changeDataSave:id_card NSString:self.changeText.text];
        }else{
            [self showHint:@"身份号证格式错误"];
        }
    }else if ([self.titleText isEqualToString:@"添加电话"]){
        NSString *url = [NSString stringWithFormat:@"%@%@",MAIN_URL,ADDPHONE_URL];
        NSDictionary *dic = @{@"member_phone":@"修改了model结构" ,@"phone":self.changeText.text};
        [NetworkRequestManager postWithUrl:url parameter:dic success:^(id requestData) {
            if ([requestData[@"code"] isEqualToString:@"20000"]) {
                [self showHint:@"修添加失败"];
            }else if ([requestData[@"code"] isEqualToString:@"10000"]){
                [self showHint:@"添加成功"];
                [NSString updateCurUserDataWithNewValue:@"phone_name" key:self.changeText.text];
                NSDictionary *dict = @{@"phone":self.changeText.text};
                [[NSNotificationCenter defaultCenter] postNotificationName:@"AddPhone" object:@"phone" userInfo:dict];
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSError *error) {
            
        }];
    }
}

-(void)changeDataSave:(NSString *)key NSString:(NSString *)value{
//    member_name  id_card self.userData[@"member_name"]
    NSString *url = [NSString stringWithFormat:@"%@%@",MAIN_URL,SaveMessage_URL];
//    NSString *str = [[NSString alloc]initWithData:self.userData[@"phone_name"][0][@"phone"] encoding:NSUTF8StringEncoding];
    NSDictionary *dic = @{@"member_phone":@"修改了model结构", key:value};
    [NetworkRequestManager postWithUrl:url parameter:dic success:^(id requestData) {
        
        if ([requestData[@"code"] isEqualToString:@"20000"]) {
            [self showHint:[NSString stringWithFormat:@"%@",requestData[@"message"]]];
        }else if ([requestData[@"code"] isEqualToString:@"10000"]){
            [self showHint:[NSString stringWithFormat:@"%@",requestData[@"message"]]];
            NSDictionary *dict = @{@"formason":self.changeText.text};
            if ([self.titleText isEqualToString:@"姓名"] ) {
                [NSString updateCurUserDataWithNewValue:@"member_name" key:self.changeText.text];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"personalData" object:@"data" userInfo:dict];
                
            }else if ([self.titleText isEqualToString:@"身份证"]){
                [NSString updateCurUserDataWithNewValue:@"id_card" key:self.changeText.text];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"personalDataID" object:@"data" userInfo:dict];
            }
            
            [self.navigationController popViewControllerAnimated:YES];
        }else if ([requestData[@"code"] isEqualToString:@"20001"]){
            NSLog(@"%@",requestData[@"message"]);
        }
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)popLastPage{
    
}


-(UITextField *)changeText{
    if (!_changeText) {
        _changeText = [[UITextField alloc]initWithFrame:CGRectMake(10, 20, ScreenW - 20, 40)];
        _changeText.textColor = [UIColor darkTextColor];
        _changeText.clearButtonMode = UITextFieldViewModeWhileEditing;
        if ([self.titleText isEqualToString:@"姓名"]) {
            //self.changeText.text = self.model.userinfo[@"member_name"];
        }else if ([self.titleText isEqualToString:@"身份证"]){
            //self.changeText.text = self.model.userinfo[@"id_card"];
        }
    }
    return _changeText;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
